<template>
    <div class="app-container">
        <el-form :model="queryParams" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="推荐人" prop="tjname">
                <el-input v-model="queryParams.tjname" placeholder="请输入推荐人" clearable style="width: 150px;"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="被推荐人" prop="nickName">
                <el-input v-model="queryParams.nickName" placeholder="请输入被推荐人" clearable style="width: 150px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="推荐人联系方式" prop="tjtel" label-width="110px">
                <el-input v-model="queryParams.tjtel" placeholder="请输入联系方式" onkeyup="value=value.replace(/[^0-9.]/g,'')"
                    clearable style="width: 150px" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="被推荐人联系方式" prop="phonenumber" label-width="130px">
                <el-input v-model="queryParams.phonenumber" onkeyup="value=value.replace(/[^0-9.]/g,'')"
                    placeholder="请输入联系方式" clearable style="width: 150px" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="推荐日期">
                <el-date-picker v-model="queryParams.startTime" type="date" value-format="timestamp"
                    :picker-options="startDatePicker" placeholder="开始时间" style="width: 150px">
                </el-date-picker>
                <el-date-picker v-model="endTimes" type="date" value-format="timestamp" :picker-options="endDatePicker"
                    placeholder="结束时间" style="width: 150px">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="userid" default-expand-all
            max-height="800" :tree-props="{ children: 'pcResponseOfTuiGuang' }">
            <el-table-column prop="tjname" label="推荐人" width="180">
            </el-table-column>
            <el-table-column prop="tjtel" label="推荐人联系方式" width="180">
            </el-table-column>
            <el-table-column prop="nickName" label="被推荐人">
            </el-table-column>
            <el-table-column prop="phonenumber" label="被推荐人联系方式">
            </el-table-column>
            <el-table-column prop="createTime" label="推荐时间">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime, '{YY}-{MM}-{DD} {hh}:{mm}:{ss}') }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="countmoney" label="总计收益">
            </el-table-column>
            <el-table-column prop="usMoney" label="返利">
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList"></pagination>
    </div>
</template>
<script>
import { findShouTuiUserIdsss } from "@/api/puser/tuiguang.js";
export default {
    name:'tuiguang',
    data() {
        return {
            endTimes: '',
            total: 0,
            showSearch: true,
            queryParams: {
                tjname: '',
                nickName: '',
                pageNum: 1,
                pageSize: 10,
                userid: this.$route.query.userid || '',
                startTime: '',
                endTime: '',
                phonenumber: '',
                tjtel: '',
            },
            tableData: [],
            startDatePicker: this.beginDate(),
            endDatePicker: this.processDate(),
        }
    },
    created() {
        this.getList();
    },
    methods: {
        async getList() {
            let res = await findShouTuiUserIdsss(this.queryParams);
            if (res.code == 200) {
                this.tableData = res.rows;
                this.total = res.total;
            }
        },
        // 搜索
        handleQuery() {
            if (this.queryParams.startTime == '' && this.endTimes == "") {
                findShouTuiUserIdsss(this.queryParams).then(response => {
                    if (response.code == 200) {
                        this.tableData = response.rows;
                        this.total = response.total;
                    }
                })
            } else if (this.queryParams.startTime == "" && this.endTimes != '') {
                this.$modal.msgError("请输入开始时间");
            } else if (this.queryParams.startTime != '' && this.endTimes == '') {
                this.$modal.msgError("请输入结束时间");
            } else if (this.queryParams.startTime != '' && this.endTimes != '' && this.queryParams.startTime > this.endTimes) {
                this.$modal.msgError("结束时间必须大于开始时间");
            } else if (this.queryParams.startTime != '' && this.endTimes != '' && this.queryParams.startTime < this.endTimes) {
                this.queryParams.endTime = this.endTimes +  24 * 60 * 60 * 1000 - 1;
                findShouTuiUserIdsss(this.queryParams).then(response => {
                    if (response.code == 200) {
                        this.tableData = response.rows;
                        this.total = response.total;
                    }
                })
            } else if (this.queryParams.startTime == this.queryParams.endTime) {
                this.$modal.msgError("结束时间必须大于开始时间");
            }
        },
        // 搜索重置
        resetQuery() {
            this.queryParams = {
                tjname: '',
                nickName: '',
                pageNum: 1,
                pageSize: 10,
                userid: this.$route.query.userid || '',
                startTime: '',
                endTime: '',
                phonenumber: '',
                tjtel: '',
            },
                this.endTimes = '';
            this.getList();
        },
        // 验证搜索框日期
        beginDate() {
            const self = this;
            return {
                disabledDate(time) {
                    //           debugger;
                    if (self.endTime) {
                        //如果结束时间不为空，则小于结束时间
                        return new Date(self.endTime).getTime() < time.getTime();
                    } else {
                        return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
                    }
                },
            };
        },
        processDate() {
            const self = this;
            return {
                disabledDate(time) {
                    if (self.startTime) {
                        //如果开始时间不为空，则结束时间大于开始时间
                        return new Date(self.startTime).getTime() > time.getTime();
                    } else {
                        return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
                    }
                },
            };
        },
    }
}
</script>